"""
file: test_login.py
Author: 城下秋草
Desc: 
"""
import json
import os.path

import pytest
from playwright.sync_api import Playwright

from pom.pages.loginPage import LoginPage
from pom.pages.productPage import ProductPage
from pom.util.util import get_json_data
from pom.util.paths import data_dir


# def get_user_data():
#     json_file = r"C:\dev\qiucao\PlayWrightCourse\pom\data\users.json"
#     with open(json_file,encoding="utf-8") as file:
#         return json.load(file)

@pytest.mark.skip
def test_login_old(playwright:Playwright):
    browser = playwright.chromium.launch(
        headless=False,
        slow_mo=500,
        args=["--window-position=800,-850"]
    )
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.saucedemo.com/")
    page.locator("#user-name").fill("standard_user")
    page.locator("#password").fill("secret_sauce")
    page.locator("#login-button").click()
    assert page.locator(".title").text_content() == "Products"

data = get_json_data(os.path.join(data_dir,"users.json"))
id_list = [d["id"] for d in data]

@pytest.mark.parametrize("data",data,ids=id_list)
def test_login(page,data):
    username = data["username"]
    password = data["password"]
    expect = data["expect"]
    login_page = LoginPage(page)
    login_page.visit("https://www.saucedemo.com")
    login_page.login(username, password)
    if login_page.error_msg:
        assert expect in login_page.error_msg
    else:
        product_page = ProductPage(page)
        assert product_page.has_correct_title

@pytest.mark.skip(reason="不再需要")
def test_fail_login(page):
    login_page = LoginPage(page)
    login_page.visit("https://www.saucedemo.com")
    login_page.login("test", "test")
    assert "Username and password do not match any user in this service" in login_page.error_msg

